cmake_minimum_required(VERSION 3.10)
project(betaflight_serial_reader VERSION 1.0)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 添加包含目录
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

# 查找并加入线程库
find_package(Threads REQUIRED)

# 在Unix平台上找到串口库
if(UNIX)
    find_package(PkgConfig REQUIRED)
    pkg_check_modules(PC_SERIAL QUIET serial)
    set(SERIAL_INCLUDE_DIRS ${PC_SERIAL_INCLUDE_DIRS})
    set(SERIAL_LIBRARIES ${PC_SERIAL_LIBRARIES})
    if(NOT PC_SERIAL_FOUND)
        # 如果没有找到串口库，可以尝试使用系统提供的串口API
        set(SERIAL_LIBRARIES "")
    endif()
elseif(WIN32)
    # Windows平台不需要额外的库，使用Windows API
    set(SERIAL_LIBRARIES "")
endif()

# 添加源文件
file(GLOB SOURCES 
    "src/*.cpp"
    "src/*.c"
)

# 创建可执行文件
add_executable(${PROJECT_NAME} ${SOURCES})

# 链接线程库和串口库
target_link_libraries(${PROJECT_NAME} 
    ${CMAKE_THREAD_LIBS_INIT}
    ${SERIAL_LIBRARIES}
)

# 安装指令
install(TARGETS ${PROJECT_NAME} DESTINATION bin)

# 打印一些配置信息
message(STATUS "Project: ${PROJECT_NAME}")
message(STATUS "Sources: ${SOURCES}")
message(STATUS "Thread libs: ${CMAKE_THREAD_LIBS_INIT}")
message(STATUS "Serial libs: ${SERIAL_LIBRARIES}") 